package com.huihui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.bean.Comment;
import com.huihui.bean.SmallVideo;
import com.huihui.bean.VideoComment;
import com.huihui.datamanager.AppManager;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.utils.ImageUtils;
import com.huihui.utils.LoadImageUtil;
import com.huihui.widget.PartColorTextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;


/**
 * Created by Administrator on 2018/8/18.
 * 详情适配器
 */

public class VideoCommentAdapter extends RecyclerView.Adapter<MyRecycleHolder> {

    private final long relation_id;
    private int class_id = 1;

    private Context mContext;
    private List<VideoComment> list;
    private MineZanGridViewAdapter adapter;
    //创建两个字段
    public final static int TYPE_HEAD = 0;
    public final static int TYPE_NORMAL = 1;
    private int isLike;

    public VideoCommentAdapter(Context context, List<VideoComment> list, long relation_id) {
        this.mContext = context;
        this.list = list;
        this.relation_id = relation_id;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();

    }

    @Override
    public MyRecycleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.douyin_pinglun_item, parent, false);
        return new MyRecycleHolder(view);
    }


    @Override
    public void onBindViewHolder(final MyRecycleHolder holder, final int position) {
        VideoComment comment = list.get(position);
        CircleImageView head_view = (CircleImageView) holder.getView(R.id.head_view);
        TextView user_name = (TextView) holder.getView(R.id.user_name);
        TextView user_time = (TextView) holder.getView(R.id.user_time);
        TextView zanCount = (TextView) holder.getView(R.id.zanCount);
        TextView comment_content = (TextView) holder.getView(R.id.comment_content);
        PartColorTextView reply_userName = (PartColorTextView) holder.getView(R.id.reply_userName);
        LinearLayout childLayout = (LinearLayout) holder.getView(R.id.child_layout);
        LoadImageUtil.displayImage(mContext, comment.getUser_head_photo(), head_view);
        final ImageView zanIcon = (ImageView) holder.getView(R.id.search_detail_zan_icon);

        final int isLike = comment.getIs_like();
        if (isLike == 1) {
            zanIcon.setImageResource(R.mipmap.zan_press);
        } else {
            zanIcon.setImageResource(R.mipmap.zan);
        }

        user_name.setText(comment.getUser_nick_name());
        user_time.setText(comment.getCom_time());
        zanCount.setText(String.valueOf(comment.getCollection_num()));
        comment_content.setText(comment.getCom_content());
        List<VideoComment> sub_comm = comment.getSub_comm();
        if (sub_comm != null && sub_comm.size() > 0) {
            childLayout.setVisibility(View.VISIBLE);
            String allText = sub_comm.get(0).getUser_nick_name() + ":" + sub_comm.get(0).getCom_content();
            reply_userName.setPartText(allText, sub_comm.get(0).getUser_nick_name(), mContext.getResources().getColor(R.color.white), mContext.getResources().getColor(R.color.white));
        } else {
            childLayout.setVisibility(View.GONE);
        }
        LinearLayout item_layout = (LinearLayout) holder.getView(R.id.item_layout);
        if (pingLunClickListener != null) {
            comment_content.setTag(position);
            comment_content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pingLunClickListener.onClick(position);
                }
            });
        }
        zanIcon.setTag(position);
        zanIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
                if (isLike == 1) {
                    removeZan(p);
                } else {
                    addZan(p);
                }
            }
        });

    }

    private OnPinglunClickListener pingLunClickListener;

    public void setPingLunClickListener(OnPinglunClickListener onItemClickListener) {
        this.pingLunClickListener = onItemClickListener;
    }

    public interface OnPinglunClickListener {
        void onClick(int position);
    }

    public void refrashData() {

    }

    private void addZan(int position) {
        final VideoComment comment = list.get(position);
        //点赞的评论ID（如果是内容点赞此处为0）
        int relation_comment_id = comment.getCom_id();
        OkHttpRequest.getInstance().addCollection(HttpContants.addCollection,
                AppManager.getUserInfo().getUser_id(),
                relation_id,
                class_id,
                relation_comment_id,
                new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
                                int collection_num = comment.getCollection_num();
                                comment.setCollection_num(++collection_num);
                                comment.setIs_like(1);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void removeZan(int p) {
        int class_id = 1;
        final VideoComment comment = list.get(p);
        long coment_id = comment.getCom_id();
        OkHttpRequest.getInstance().removeCollection(HttpContants.removeCollection, AppManager.getUserInfo().getUser_id(),
                relation_id, class_id, (int) coment_id, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
                                int collection_num = comment.getCollection_num();
                                comment.setCollection_num(--collection_num);
                                comment.setIs_like(0);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
